<?php
/*
+--------------------------------------------------------------------------
|   phpBIZbiz v3.0 full version
|   ========================================
|   by taft@wjl.cn
|   http://www.phpbiz.cn
|   all rights reserved
+---------------------------------------------------------------------------
|
|   > 
|   > 最后修改日期：2005-9-9
|   > 
|
+--------------------------------------------------------------------------
*/
!function_exists('do_login') && exit('Forbidden');
$member = new ad_member;

class ad_member
{
	var $base_url;
	var $upload_dir;
	var $upload_url;

    function ad_member()
    {
		global $INCOME,$CONF,$ADMIN,$ADMINER;

        $this -> upload_dir = $CONF['upload_dir'];
		
		//---------------------------------------
		// Kill globals - globals bad, Homer good.
		//---------------------------------------
		
		$tmp_in = array_merge( $_GET, $_POST, $_COOKIE );
		
		foreach ( $tmp_in as $k => $v )
		{
			unset($$k);
		}

        //---------------------------------------

		switch( $INCOME['code'] )
		{
			case 'add' :
			   $this -> add_form();    //预注册
			   break;
		    case 'findshop' :
			   $this -> do_search(0);
		       break;
			case 'adfind' :
			   $this -> do_search(1);
			   break;
			//+-------------------
			case  'del' :
			   $this -> del_form();
			   break;
			case 'dodel' :
				$this -> do_del();
			   break;
			case 'delsearch':
				$this -> del_search();
			   break;
			//+-------------------
			case 'doform' :
				$this -> do_form(0);  
			   break;
			case 'modify' :
				$this -> do_form(1);
			   break;
			//+-------------------
			case 'check' :
				$this -> check_form();
			   break;
			case 'glist':
				$this -> goods_list();
			   break;
			case 'galist':
				$this -> goods_all_list();
			    break;
			case 'delgoods':
				$this -> del_goods();
			    break;
			case 'delpic':
				$this -> del_pic();
			    break;
			case 'list':
				$this -> list_form();     // list all
			    break;
			case 'recom':
				$this -> recom_form(1);   // diamond
			    break;
			case 'trust':
				$this -> recom_form(2);   // trust
			    break;
			case 'dorecom':
				$this -> do_recom();
				break;
			case 'banshop':
				$this -> do_banshop(1);    // trust
			     break;
		    case 'settrust':
				 $this -> do_setshop(2);		
		    case 'settop':
				$this -> do_setshop(1);   // diamond
			     break;
			case 'vgoods':
				 $this -> view_goods();
			     break;
			case 'deditgoods':
			     $this -> do_edit_goods();
				 break;
		    case 'index':
				 $this -> index();
			     break;
			default :
			   $this -> search_form();
		}
	}

	function search_form()
	{
		global $ADMIN,$SKIN;

		$ADMIN -> page_title = "查找/编辑/暂停商铺";
		$ADMIN -> page_instruction ="搜索到目标商铺后选择相应的操作";

		//+----------------- fast form --------------
		
		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );
		
		$ADMIN->html .= $SKIN->table_head( "查找商家所开的店铺" );
		
		$ADMIN->html .= $SKIN->form_head();

		$ADMIN->html .= $SKIN->form_input( "act","shop",'hidden');
		$ADMIN->html .= $SKIN->form_input( "code","findshop",'hidden');
									     
		$ADMIN->html .= $SKIN->table_row( array( "<b>键入完整的商家名</b>" ,
												  $SKIN->form_input( "SELLER_NAME")  // log_accout
									     )      );
									     
		$ADMIN->html .= $SKIN->form_end("查找商铺");
										 
		$ADMIN->html .= $SKIN->table_end();

		$ADMIN->html .= $SKIN->td_spacer();

       //+---------------- advance form --------------
       	   		
	   $SKIN->td_header[] = array( "&nbsp;"  , "40%" );
	   $SKIN->td_header[] = array( "&nbsp;"  , "60%" );

       $ADMIN->html .= $SKIN->table_head( "商铺高级搜索","请至少填写一项，留空的项将在查询中忽略" );

	   $ADMIN->html .= $SKIN->form_head();
       $ADMIN->html .= $SKIN->form_input( "act","shop",'hidden');
       $ADMIN->html .= $SKIN->form_input( "code","adfind",'hidden');

	   									     
		$ADMIN->html .= $SKIN->table_row( array( "<b>商铺名包含</b>" ,
												  $SKIN->form_input( "shop_name" )  // different from above
									     )      );
									     
		$ADMIN->html .= $SKIN->table_row( array( "<b>Email地址包含</b>" ,
												  $SKIN->form_input( "shop_email_address" )
									     )      );
									     
		$ADMIN->html .= $SKIN->table_row( array( "<b>所在地址包含</b>" ,
												  $SKIN->form_input( "shop_location" )
									     )      );
				                         
		$ADMIN->html .= $SKIN->table_row( array( "<b>电话号码包括</b>" ,
												  $SKIN->form_input( "shop_telephone" )
									     )      );

		$ADMIN->html .= $SKIN->table_row( array( "<b>商铺描述包含</b>" ,
												  $SKIN->form_input( "shop_describle" )
									     )      );
		$ADMIN->html .= $SKIN->table_row( array( "<b>商铺推荐度 大于等于 </b>" ,
												  $SKIN->form_input( "shop_bg_rate" )
									     )      );
		$ADMIN->html .= $SKIN->table_row( array( "<b>商铺推荐度 小于等于 </b>" ,
												  $SKIN->form_input( "shop_le_rate" )
									     )      );

        $ADMIN->html .= $SKIN->form_end("搜索商铺");
										 
		$ADMIN->html .= $SKIN->table_end();
		
		$ADMIN->output();
	}

	function do_search( $advance )
    {
		global $DB,$ADMIN,$SKIN,$INCOME;

		if( $advance )
		{ // perform advance search
			$query_condition = array();
			
			foreach( array( 'shop_name','shop_telephone','shop_email_address','shop_location','shop_describle' ) as $bit )
			{
				$INCOME[ $bit ] = urldecode(trim($INCOME[ $bit ]));
				
				$page_query .= '&'.$bit.'='.urlencode($INCOME[ $bit ]);
				
				if ($INCOME[ $bit ] != "")
				{
					$query_condition[] = 's.'.$bit." LIKE '%".$INCOME[$bit]."%'";
				}
			}

			foreach( array('shop_bg_rate','shop_le_rate') as $key )
			{   
				$INCOME[$key] = $INCOME[$key] ==""? 0 : $INCOME[$key];   // for the blank
				if ( !is_numeric($INCOME[$key]) )
						$ADMIN->error("推荐度必须为数字");
			}

			if( $INCOME['shop_bg_rate'] !="" ) 
				 $query_condition[] = 's.shop_global_recommend >='.$INCOME['shop_bg_rate']; 
            if( $INCOME['shop_le_rate'] !="" ) 
				 $query_condition[] = 's.shop_global_recommend <='.$INCOME['shop_le_rate'];
			
			if (count($query_condition) < 1)
			{
				$ADMIN->error("提交搜索表单前请至少完成一项");
			}
			
			$rq = implode( " AND ", $query_condition );            

		}else{
		    //simple
            $INCOME['SELLER_NAME'] = trim($INCOME['SELLER_NAME']);
			if ($INCOME['SELLER_NAME'] == "")
			{
					$ADMIN->error("未输入要搜索的商家名！");
			}

			$DB -> db_query( "SELECT seller_id from biz_seller WHERE seller_logaccount ='".$INCOME['SELLER_NAME']."'" ) ;

			$seller = $DB -> db_fetch_row();

			$page_query = "&SELLER_NAME=".urlencode($INCOME['SELLER_NAME']);

			$rq = "seller_id = '".$seller['seller_id']."'";
		}
		
		$start = $INCOME['st'] ? intval( $INCOME['st'] ) : 0;
		
		$query = "SELECT s.shop_id, s.shop_email_address, s.shop_name, s.shop_location, s.shop_telephone ,s.shop_describle,s.shop_ban_status,s.shop_check_status
		          FROM biz_shop s
		          WHERE $rq ORDER BY s.shop_create_time DESC LIMIT $start,10";

		$DB->db_query("SELECT COUNT(s.shop_id) as count FROM biz_shop s WHERE $rq");

		 $row_count = $DB -> db_fetch_row();

		if ($row_count['count'] < 1)
				$ADMIN->error("你的搜索查询没有从用户数据库中返回任何匹配。请返回并重试");
  		
		$ADMIN->page_title = "本次商铺搜索结果";
		$ADMIN->page_detail = "搜索结果。";

		$pages = $SKIN->build_pagelinks( array('TOTAL_POSS'  => $row_count['count'],
											   'PER_PAGE'    => 10,
											   'CUR_ST_VAL'  => $start,
											   'L_SINGLE'    => "一页",
											   'L_MULTI'     => "页：",
											   'BASE_URL'    => $this->base_url."?act=shop"."&code={$INCOME['code']}".$page_query
											   )
											);
		$SKIN->td_header[] = array( "地址"  , "15%" );
		$SKIN->td_header[] = array( "类别"  , "15%" );
		$SKIN->td_header[] = array( "是否通过审查" , "10%" );
		$SKIN->td_header[] = array( "是否被禁止"   , "11%" );
		$SKIN->td_header[] = array( " "    , "11%" );
		$SKIN->td_header[] = array( " "    , "12%" );
		$SKIN->td_header[] = array( " "    , "11%" );
		$SKIN->td_header[] = array( " "    , " " );
		$SKIN->td_header[] = array( " ", " ");
	
	    $ADMIN->html .= $SKIN->table_head( "{$row_count['count']} "."条结果" );

		$DB->db_query($query);
		
		while ( $r = $DB->db_fetch_row() )
		{
			
			$ADMIN->html .= $SKIN->td_basic( "<img src='{$SKIN -> img_url}/item.gif' border='0' alt='-'>&nbsp;{$r['shop_name']}", "left", "pformstrip" );
			$ADMIN->html .= $SKIN->table_row( array( "{$r['shop_location']}",
													 "{$r['shop_name']}",
													  $r['shop_check_status'],
													  "<center>".$r['shop_ban_status']."</center>",
													  "<center><strong><a href='{$this->base_url}?act=shop&code=doform&SHOP_ID={$r['shop_id']}' title='Edit this shop's account'>编辑细节</a></strong></center>",
													  "<center><strong><a href='{$this->base_url}?act=shop&code=glist&SHOP_ID={$r['shop_id']}' title='Edit this shop's account'>查看所属商品</a></strong></center>",
													  "<center><a href='{$this->base_url}?act=shop&code=banshop&shop_ID={$r['shop_id']}' title='Suspend Member'>禁止商铺</a></span></center>",
														  "<a href='{$this->base_url}?act=shop&code=settop&shop_ID={$r['shop_id']}'>钻石商铺</a>","<a href='{$this->base_url}?act=shop&code=settrust&shop_ID={$r['shop_id']}'>诚信商铺</a>"
									     	 )      );
		}

		$ADMIN->html .= $SKIN->td_basic($pages, 'right', 'pformstrip');
		$ADMIN->html .= $SKIN->table_end();
		$ADMIN->output();
	}

	function del_form()
	{
		global $ADMIN,$SKIN,$DB;

		$ADMIN->page_title = "商铺删除";
		$ADMIN->page_instruction = "键入部分或全部的商铺名。";
		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );
		
		$ADMIN->html .= $SKIN->table_head( "商铺查找" );
		$ADMIN->html .= $SKIN->form_head();
		$ADMIN->html .= $SKIN->form_input("act","shop","hidden");
		$ADMIN->html .= $SKIN->form_input("code","delsearch","hidden");
									     
		$ADMIN->html .= $SKIN->table_row( array( "<b>键入部分或全部的商铺名</b>" ,
												  $SKIN->form_input( "SHOP_NAME" )  
									     )      );
									     
		$ADMIN->html .= $SKIN->form_end("查找商铺");
 		$ADMIN->html .= $SKIN->table_end();
		$ADMIN->output();
	}

	function del_search()
	{
		global $DB,$ADMIN,$SKIN,$INCOME;

		if( !$INCOME['SHOP_NAME'] ) $ADMIN -> error( "你没有输入商铺名！" );

		$DB->db_query("SELECT shop_id, shop_name FROM biz_shop WHERE shop_name LIKE '%".$INCOME['SHOP_NAME']."%'" );

		if (! $DB->db_fetch_num() )
		{
			$ADMIN->error("对不起，找不到任何匹配你键入的搜索字符串的商家");
		}
		
		while ( $r = $DB->db_fetch_row() )
		{
			$form_array[] = array( $r['shop_id'] , $r['shop_name'] );
		}

		
		$ADMIN -> page_title = "商铺删除";
		$SKIN->td_header[] = array( "&nbsp;"  , "40%" );
		$SKIN->td_header[] = array( "&nbsp;"  , "60%" );

		$ADMIN ->html .= $SKIN->table_head("搜索结果","注意：一旦删除商铺成功，此商铺所属的相关信息都将被删除，请小心操作！");

		$ADMIN -> html .= $SKIN->form_head();
		$ADMIN -> html .= $SKIN->form_input("act","shop","hidden");
		$ADMIN -> html .= $SKIN->form_input("code","dodel","hidden");
        
		$ADMIN->html .= $SKIN->table_row( array( "<b>从匹配的……选择</b>" ,
												  $SKIN->form_droplist( "SHOP_ID", $form_array )
									     )      );
		$ADMIN->html .= $SKIN->form_end("删除");
		$ADMIN->html .= $SKIN->table_end();
		$ADMIN-> output();

	}

	function do_del()
	{
		global $DB,$INCOME,$ADMIN,$CONF;

		$pic_file = array();

		$id =intval($INCOME['SHOP_ID']);

		$DB -> db_query( "SELECT shop_name as n FROM biz_shop WHERE shop_id = '$id' LIMIT 1" );

		$shop = $DB -> db_fetch_row();
         
		if( $DB->db_fetch_num()<1 )
			  $ADMIN -> error( "查询数据出错！" );
	    //-----------------------------------------------
		//del message and pic 
		//-----------------------------------------------

		$res = $DB -> db_query( " SELECT goods_id as g FROM biz_goods WHERE shop_id = '$id' ");
        while( $ar = $DB -> db_fetch_row( $res ) )
		{
			$DB -> db_query( "DELETE FROM biz_message WHERE message_owner_goods_id = '$ar[g]' OR message_owner_shop_id = '$id' " );
			
			$DB -> db_query( "SELECT pic_name as n, pic_id as i FROM biz_pic WHERE pic_goods_id ='".$ar['g']."' OR pic_shop_id ='$id'");
		    while( $p = $DB -> db_fetch_row() )
		        if($p['i'] != 1 ) { $pic_file[] = $p['n'];  $pic_thumb[] = $CONF['thumb_prefix'].$p['n']; }
		    			
			$DB -> db_query( "DELETE FROM biz_pic WHERE pic_goods_id = '$ar[g]' OR pic_shop_id ='$id' " );
		}

		$DB -> db_query( "DELETE FROM biz_message WHERE  message_owner_shop_id = '$id' " );
		
		$DB -> db_query( "SELECT pic_name as n , pic_id as i FROM biz_pic WHERE pic_shop_id ='$id'" );
		while( $p  = $DB-> db_fetch_row() )
			                     if($p['i'] != 1 ) { $pic_file[] = $p['n'];  $pic_thumb[] = $CONF['thumb_prefix'].$p['n']; }
		$ADMIN -> unlink_pic( $pic_file );
     	$ADMIN -> unlink_pic( $pic_thumb,1);
		$DB -> db_query( "DELETE FROM biz_pic WHERE  pic_shop_id ='$id' " );

		$DB -> db_query( "DELETE FROM biz_goods WHERE shop_id = '$id' " );
        $DB -> db_query( "DELETE FROM biz_shop WHERE shop_id = '$id' ");

		$ADMIN -> record_log( "删除商铺‘<b>{$shop[n]}</b>’");
		$ADMIN -> done_screen( "商铺及相关信息已删除", "用户控制", "act=shop&code=edit" );
	
	}

	function do_form( $edit )
	{
		global $ADMIN,$INCOME,$DB,$SKIN,$CONF;

		$id = intval( $INCOME[SHOP_ID] );

		if( $edit )
		{   //if submitted!
			$db_string = $DB-> db_compile_update_fields ( array(  "shop_name"      => $INCOME[shop_name] ,
				                                          "shop_location"  => $INCOME[shop_location],
				                                          "shop_telephone" => $INCOME[shop_telephone],
				                                          "shop_ban_status" => $INCOME[shop_ban_status] =='on'? 'on':'off',
				                                          "shop_check_status" => $INCOME[shop_check_status] =='on'? 'checked':'uncheck',
														  "shop_email_address" => $INCOME[shop_email_address],
				                                          "shop_global_recommend" => $INCOME[shop_global_recommend],
			                                              "shop_describle" => $INCOME[shop_describle],
														  "shop_catalog" =>$INCOME[shop_catalog]                  
			)); 
			$db_query = "UPDATE biz_shop SET $db_string WHERE shop_id='".$id."'";

			$DB -> db_query($db_query);

			$DB -> db_query( " select shop_name as a from biz_shop WHERE shop_id= '$id' " );
			$account = $DB -> db_fetch_row();
             
			$ADMIN->record_log("修改商铺信息‘{$account[a]}’");
	        $ADMIN->done_screen("商铺信息已修改", "用户控制", "act=shop&code=edit" );

		}
		
		$DB -> db_query( " select s.*, p.pic_name from biz_shop s, biz_pic p WHERE s.shop_id = '$id' and p.pic_id =s.shop_prime_picture_id LIMIT 0,1" );
		$shop_detail = $DB -> db_fetch_row();

		$DB -> db_query( " select count(*) as num from biz_goods where shop_id = '$id' ");
		$seller_shop = $DB -> db_fetch_row();

		$ADMIN ->page_title = "本次商铺搜索结果";
		$ADMIN ->page_instruction ="修改商铺信息";

		$SKIN -> td_header[] = array(" ","30%");
		$SKIN -> td_header[] = array(" ","");

		$ADMIN -> html .= $SKIN -> table_head(" ");
        $ADMIN -> html .= $SKIN -> form_head();
		$ADMIN -> html .= $SKIN -> form_input( "act","shop","hidden");
        $ADMIN -> html .= $SKIN -> form_input( "code","modify","hidden" );
		$ADMIN -> html .= $SKIN -> form_input( "SHOP","{$id}","hidden");

		$ADMIN -> html .= $SKIN -> table_row( array( "商铺名称", $SKIN -> form_input( "shop_name","{$shop_detail[shop_name]}" )));
		$ADMIN -> html .= $SKIN -> table_row( array( "商铺图片","<img src='".$ADMIN->get_thumb($shop_detail[pic_name])."'>") );
		$ADMIN -> html .= $SKIN -> table_row( array( "此商铺里面的商品数目", $seller_shop['num'] ));
		$ADMIN -> html .= $SKIN -> table_row( array( "商铺描述", "<TEXTAREA NAME='shop_describle' ROWS='8' >{$shop_detail[shop_describle]}</TEXTAREA>") );
		$ADMIN -> html .= $SKIN -> table_row( array( "商铺分类（逗号隔开）", $SKIN -> form_input("shop_catalog","$shop_detail[shop_catalog]") ));

		$ADMIN -> html .= $SKIN -> table_row( array( "商铺地址", $SKIN -> form_input( "shop_location","{$shop_detail[shop_location]}" )));
		$ADMIN -> html .= $SKIN -> table_row( array( "商铺联系电话", $SKIN -> form_input( "shop_telephone","{$shop_detail[shop_telephone]}" )));
		$ADMIN -> html .= $SKIN -> table_row( array( "商铺Email", $SKIN -> form_input( "shop_email_address","{$shop_detail[shop_email_address]}" )));
		$ADMIN -> html .= $SKIN -> table_row( array( "商铺推荐度", $SKIN -> form_input( "shop_global_recommend","{$shop_detail[shop_global_recommend]}" )));
       
		$ban_stat = ( $shop_detail[shop_ban_status] == 'on' ) ? 'checked' : '';
		$ADMIN -> html .= $SKIN -> table_row( array( "商铺是否审禁止（打勾表示禁止）", "<input type='checkbox' name='shop_ban_status' $ban_stat>"));
		$check_stat = ( $shop_detail[shop_check_status] == 'checked' ) ? 'checked' : '';
		$ADMIN -> html .= $SKIN -> table_row( array( "商铺是否审通过审查（打勾表示通过）", "<input type='checkbox' name='shop_check_status' $check_stat>"));

		$ADMIN -> html .= $SKIN -> form_end( "修改提交" );
		$ADMIN -> html .= $SKIN -> table_end();

		$ADMIN -> output();


	}

	function check_form()
	{
		global $ADMIN, $DB, $SKIN, $INCOME;

		$sql = " SELECT COUNT(*) as ct FROM biz_shop WHERE shop_check_status = 'uncheck' ";

		$DB -> db_query( $sql );

		$row_count = $DB -> db_fetch_row();

		if (  $row_count < 1 )
				$ADMIN->error("没有未审批的商铺。");


		$start = $INCOME['st'] ? intval( $INCOME['st'] ) : 0;
		
		$query = " SELECT * FROM biz_shop WHERE shop_check_status = 'uncheck' LIMIT $start,10 ";

		$pages = $SKIN->build_pagelinks( array('TOTAL_POSS'  => $row_count['ct'],
											   'PER_PAGE'    => 10,
											   'CUR_ST_VAL'  => $start,
											   'L_SINGLE'    => "一页",
											   'L_MULTI'     => "页：",
											   'BASE_URL'    => $this->base_url."?act=shop"."&code={$INCOME['code']}"
											   )
											);


		$ADMIN -> page_title = "商铺批准";
		$ADMIN -> page_instruction = "这里列出了最近注册但还未审批通过的商铺";


		$SKIN->td_header[] = array( "所属商家"  , "15%" );
		$SKIN->td_header[] = array( "电话号码" , "10%" );
		$SKIN->td_header[] = array( "Email"   , "15%" );
		$SKIN->td_header[] = array( "查看详细"    , "16%" );
	
	    $ADMIN->html .= $SKIN->table_head( "{$row_count['ct']} "."条结果" );

		$res = $DB->db_query( $query );
		
		while ( $r = $DB->db_fetch_row( $res ) )
		{
			
            $DB -> db_query( "SELECT seller_logaccount as a FROM biz_seller WHERE seller_id = '$r[seller_id]'" );
			$sel = $DB ->db_fetch_row();

			$ADMIN->html .= $SKIN->td_basic( "<img src='{$SKIN -> img_url}/item.gif' border='0' alt='-'>&nbsp;{$r['shop_name']}", "left", "pformstrip" );
			$ADMIN->html .= $SKIN->table_row( array(
													  $sel[a],
			                                          $r['shop_telephone'],
													  "<center>".$r['shop_email_address']."</center>",
													  "<center><strong><a href='{$this->base_url}?act=shop&code=doform&SHOP_ID={$r['shop_id']}' title='Edit this shop's account'>查看详细</a></strong></center>",
													  )      );
		}

		$ADMIN->html .= $SKIN->td_basic($pages, 'right', 'pformstrip');
										 
		$ADMIN->html .= $SKIN->table_end();
		
		$ADMIN->output();
	}

	function goods_list()
	{
		global $INCOME,$DB,$ADMIN,$SKIN;
        
		$id = intval($INCOME['SHOP_ID']);
		
		$DB -> db_query( "SELECT * FROM biz_goods WHERE shop_id ='".$id."'" );

		if( !$DB -> db_fetch_num() )
                    $ADMIN -> error("该商铺还未登陆商品！");		
		
		$ADMIN -> page_title ="商品列表";

		$SKIN -> td_header[] = array("商品名","24%");
		$SKIN -> td_header[] = array("商品上架时间","24%");
		$SKIN -> td_header[] = array("商品有效日期","20%");
        $SKIN -> td_header[] = array(" ","12%");
		$SKIN -> td_header[] = array(" ","12%");
		$SKIN -> td_header[] = array(" ","");
		
		$ADMIN -> html .= $SKIN -> table_head();

		while( $r = $DB -> db_fetch_row() )
		{
			
			$date = date('Y-m-d H:i:s',$r['goods_store_date']);

			$ADMIN -> html .= $SKIN -> table_row( array("<center><b>{$r['goods_name']}</b></center>",
				                                        "<div align='right'>{$date}</div>",
				                                        "<div align='right'>{$r['goods_valid_period']}天</div>",
				                                        "<center><a href='{$this->base_url}?act=shop&code=vgoods&GOODS_ID={$r['goods_id']}' >查看详细</a></center>",
														"<center><a href='{$this->base_url}?act=shop&code=delpic&GOODS_ID={$r['goods_id']}' title='Delete pic'>移出图片</a></center>",
				                                        "<center><a href='{$this->base_url}?act=shop&code=delgoods&GOODS_ID={$r['goods_id']}' title='Delete goods'>删除商品</a></center>") );
		}

		$ADMIN -> html .= $SKIN -> table_end();

		$ADMIN -> output();

	}

	function del_goods()
	{
		global $DB,$ADMIN,$INCOME;

		$id =intval($INCOME['GOODS_ID']);

		$DB -> db_query("SELECT * FROM biz_goods WHERE goods_id='".$id."'");
        $g_r = $DB -> db_fetch_row();
		
		if( !$DB -> db_fetch_num() )
			$ADMIN -> error("不存在该商品记录");

		$r = $DB -> db_fetch_row();

		$DB -> db_query("DELETE FROM biz_message WHERE message_owner_goods_id ='".$id."'");

		$DB -> db_query("SELECT  pic_name FROM biz_pic WHERE pic_goods_id ='".$id."'" );

		$r_p = $DB -> db_fetch_row();

		$p_name = array( $r_p['pic_name'] );

		$ADMIN -> unlink_pic( $p_name );
		$ADMIN -> unlink_pic( array( $CONF['thumb_prefix'].$r_p['pic_name'] ),1);

		$DB -> db_query("DELETE FROM biz_pic WHERE pic_goods_id ='".$id."'");

		$DB -> db_query("DELETE FROM biz_goods WHERE goods_id='".$id."'");

		$ADMIN->record_log("删除商品‘{$g_r[goods_name]}’");
	    $ADMIN->done_screen("商品‘{$g_r[goods_name]}’已删除", "商铺商品管理", "act=shop&code=edit" );

	}

	function del_pic()
	{
		global $DB,$INCOME,$ADMIN,$CONF;

		$id =intval($INCOME['GOODS_ID']);

		$DB -> db_query("SELECT * FROM biz_goods WHERE goods_id='".$id."'");
        $g_r = $DB -> db_fetch_row();
		
		if( !$DB -> db_fetch_num() )
			$ADMIN -> error("不存在该商品记录");

		$DB -> db_query("SELECT  pic_name FROM biz_pic WHERE pic_goods_id ='".$id."'" );

		$r_p = $DB -> db_fetch_row();

		$p_name = array( $r_p['pic_name'] );

		$ADMIN -> unlink_pic( $p_name );
        $ADMIN -> unlink_pic( array( $CONF['thumb_prefix'].$r_p['pic_name'] ),1);

		$DB -> db_query("DELETE FROM biz_pic WHERE pic_goods_id ='".$id."'");

		
		$DB -> db_query( "UPDATE biz_goods SET `goods_prime_picture_id` = '0' WHERE `goods_id` ='".$id."' LIMIT 1" );

		$ADMIN->record_log("移除商品图片‘{$g_r[goods_name]}’");
	    $ADMIN->done_screen("商品‘{$g_r[goods_name]}’图片已移除", "商铺商品管理", "act=shop&code=edit" );

	}

	function list_form()
	{
		global $ADMIN,$SKIN,$DB,$INCOME;
	    
		$ADMIN-> page_title = "所有商铺列表";

		$start = $INCOME['st'] ? intval( $INCOME['st'] ) : 0;
		
		$query = "SELECT s.shop_id, s.shop_email_address, s.shop_name, s.shop_location, s.shop_telephone ,s.shop_describle,s.shop_ban_status,s.shop_check_status
		          FROM biz_shop s
		          WHERE 1 ORDER BY s.shop_create_time DESC LIMIT $start,10";

		$DB -> db_query( "SELECT count(*) as ct FROM biz_shop" );

		$row_count = $DB -> db_fetch_row();

		$pages = $SKIN->build_pagelinks( array('TOTAL_POSS'  => $row_count['ct'],
											   'PER_PAGE'    => 10,
											   'CUR_ST_VAL'  => $start,
											   'L_SINGLE'    => "一页",
											   'L_MULTI'     => "页：",
											   'BASE_URL'    => $this->base_url."?act=shop"."&code={$INCOME['code']}".$page_query
											   )
											);

		$SKIN->td_header[] = array( "地址"  , "15%" );
		$SKIN->td_header[] = array( "类别"  , "15%" );
		$SKIN->td_header[] = array( "是否通过审查" , "8%" );
		$SKIN->td_header[] = array( "是否被禁止"   , "8%" );
		$SKIN->td_header[] = array( " "    , "11%" );
		$SKIN->td_header[] = array( " "    , "12%" );
		$SKIN->td_header[] = array( " "    , "11%" );
		$SKIN->td_header[] = array( " "    , " " );
		$SKIN->td_header[] = array( " "    , " " );
	
	    $ADMIN -> html .= $SKIN -> table_head();

		$DB -> db_query($query);
	    
		while ( $r = $DB->db_fetch_row() )
		{
			
			$ADMIN->html .= $SKIN->td_basic( "<img src='{$SKIN -> img_url}/item.gif' border='0' alt='-'>&nbsp;{$r['shop_name']}", "left", "pformstrip" );
			$ADMIN->html .= $SKIN->table_row( array( "{$r['shop_location']}",
													 "{$r['shop_name']}",
													  $r['shop_check_status'],
													  "<center>".$r['shop_ban_status']."</center>",
													  "<center><strong><a href='{$this->base_url}?act=shop&code=doform&SHOP_ID={$r['shop_id']}' title='Edit this shop's account'>编辑细节</a></strong></center>",
													  "<center><strong><a href='{$this->base_url}?act=shop&code=glist&SHOP_ID={$r['shop_id']}' title='Edit this shop's account'>查看所属商品</a></strong></center>",
													  "<center><a href='{$this->base_url}?act=shop&code=banshop&shop_ID={$r['shop_id']}' title='Suspend Member'>禁止商铺</a></span></center>",
														  "<a href='{$this->base_url}?act=shop&code=settop&shop_ID={$r['shop_id']}'>钻石商铺</a>","<a href='{$this->base_url}?act=shop&code=settrust&shop_ID={$r['shop_id']}'>诚信商铺</a>"
									     	 )      );
		}

		$ADMIN->html .= $SKIN->td_basic($pages, 'right', 'pformstrip');
										 
		$ADMIN->html .= $SKIN->table_end();
		
		$ADMIN->output();
	
	}

	function  recom_form($type)
	{

		global $INCOME,$DB,$ADMIN,$SKIN;

		$DB -> db_query("SELECT * FROM biz_shop WHERE shop_set_top = ".$type." ORDER BY shop_global_recommend DESC");
		if( !$DB->db_fetch_num() ) $ADMIN -> error("尚未设置推荐商铺！");
		$ADMIN -> page_instruction =" ";
		$ADMIN -> page_title = $type == 1 ? "钻石商铺推荐" : "诚信商铺推荐";
		$ADMIN -> page_instruction = "前台按照设定‘推荐值’降序排序";

		$SKIN -> td_header[] = array("商铺名称","");
		$SKIN -> td_header[] = array("人气指数","");
		$SKIN -> td_header[] = array("开业时间","");
		$SKIN -> td_header[] = array("推荐值","");
		$SKIN -> td_header[] = array("取消推荐","");

		$ADMIN -> html = $SKIN -> table_head();
		$ADMIN -> html .= $SKIN -> form_head();
		$ADMIN -> html .= $SKIN -> form_input("code","dorecom","hidden");
		while( $r=$DB -> db_fetch_row() )
		{
			$tm = date('Y-m-d H:i:s',$r['shop_create_time']);
			$ADMIN -> html .= $SKIN -> table_row( array("<b>{$r['shop_name']}</b>","{$r['shop_visit_count']}","{$tm}","<div align='center'>".$SKIN->form_input("rank_".$r[shop_id],$r[shop_global_recommend],6)."</div>",$SKIN->form_input("unkeep_".$r['shop_id'],"1","checkbox")."</center>") );
		}
		$ADMIN -> html .= $SKIN -> form_end("提交修改");
		$ADMIN -> html .= $SKIN -> table_end();
		$ADMIN -> output();
		return;
	
	}

	function do_recom()   // shop
	{
		global $INCOME,$DB,$ADMIN;

		foreach( $INCOME as $k => $v)
		{
			$kk = explode("_",$k);
			if($kk[0]=='rank')
			{
				
				$DB -> db_query(" UPDATE biz_shop SET shop_global_recommend =".intval($INCOME[$k])." WHERE shop_id =".$kk[1]);
			}
			elseif($kk[0]=='unkeep')
			{
				$DB -> db_query(" UPDATE biz_shop SET shop_global_recommend =0,shop_set_top=0 WHERE shop_id=".$kk[1]);
			}
				
		}
		$ADMIN->recache();
		$ADMIN->record_log("调整首页商铺");
	    $ADMIN->done_screen("推荐管理","商铺商品管理", "act=shop&code=recom" );
		
		return;
	}

	function do_banshop()
	{
		global $INCOME,$ADMIN,$DB;

		$id = intval($INCOME['shop_ID']);

		$DB -> db_query("SELECT * FROM biz_shop WHERE shop_id='".$id."'");
        $s_r = $DB -> db_fetch_row();
		
		if( !$DB -> db_fetch_num() )
			$ADMIN -> error("不存在该商铺记录");

		$DB -> db_query("UPDATE biz_shop SET shop_ban_status ='on' WHERE shop_id ='".$id."'");
		
		$ADMIN->record_log("禁止商铺‘{$s_r[shop_name]}’");
	    $ADMIN->done_screen("商铺‘{$s_r[shop_name]}’已禁止", "商铺商品管理", "act=shop&code=edit" );
	}
	
	function goods_all_list()
	{
		global $INCOME,$ADMIN,$DB,$SKIN;

		$start = $INCOME['st'] ? intval( $INCOME['st'] ) : 0;
		
		$query = "SELECT * FROM biz_goods WHERE 1 ORDER BY goods_store_date DESC LIMIT $start,20";

		$DB -> db_query( "SELECT count(*) as ct FROM biz_goods" );

        $row_count = $DB -> db_fetch_row();

		$pages = $SKIN->build_pagelinks( array('TOTAL_POSS'  => $row_count['ct'],
											   'PER_PAGE'    => 20,
											   'CUR_ST_VAL'  => $start,
											   'L_SINGLE'    => "一页",
											   'L_MULTI'     => "页：",
											   'BASE_URL'    => $this->base_url."?act=shop"."&code={$INCOME['code']}".$page_query
											   )
											);
		
		$ADMIN -> page_title ="所有商品列表";

		$SKIN -> td_header[] = array("商品名","24%");
		$SKIN -> td_header[] = array("商品上架时间","24%");
		$SKIN -> td_header[] = array("商品有效日期","20%");
        $SKIN -> td_header[] = array(" ","12%");
		$SKIN -> td_header[] = array(" ","12%");
		$SKIN -> td_header[] = array(" ","");
		
		$ADMIN -> html .= $SKIN -> table_head("共".$row_count['ct']."件商品");

		$DB -> db_query($query);

		while( $r = $DB -> db_fetch_row() )
		{
			
			$date = date('Y-m-d H:i:s',$r['goods_store_date']);

			$ADMIN -> html .= $SKIN -> table_row( array("<center><b>{$r['goods_name']}</b></center>",
				                                        "<div align='right'>{$date}</div>",
				                                        "<div align='right'>{$r['goods_valid_period']}天</div>",
				                                        "<center><a href='{$this->base_url}?act=shop&code=vgoods&GOODS_ID={$r['goods_id']}' >查看详细</a></center>",
														"<center><a href='{$this->base_url}?act=shop&code=delpic&GOODS_ID={$r['goods_id']}' title='Delete pic'>移出图片</a></center>",
				                                        "<center><a href='{$this->base_url}?act=shop&code=delgoods&GOODS_ID={$r['goods_id']}' title='Delete goods'>删除商品</a></center>") );
		}
     
		$ADMIN->html .= $SKIN->td_basic($pages, 'right', 'pformstrip');

		$ADMIN -> html .= $SKIN -> table_end();

		$ADMIN -> output();
	}

	function do_setshop($type)
	{
		global $INCOME,$DB,$ADMIN;
        
		/*/---------------------------------------
		//first remove the exiting top star or trust shop
		//---------------------------------------

		$resouce = $DB -> db_query("SELECT shop_id FROM biz_shop WHERE shop_set_top ='".$type."'");
        
		while($result = $DB -> db_fetch_row($resouce))
		{
			if($result['shop_id']) $DB -> db_query("UPDATE biz_shop SET shop_set_top = 0 WHERE shop_id='".$result['shop_id']."'");
		}
         */
		//---------------------------------------
		//then update
		//---------------------------------------

		$id = intval($INCOME['shop_ID']);

		$DB -> db_query("UPDATE biz_shop SET shop_set_top = '".$type."' WHERE shop_id='".$id."'");

		//write log
				
		$msg = ($type==1)?"设置新的钻石商铺":"设置新的诚信商铺";
		$ADMIN->record_log($msg);
	    $msg =($type==1)?"钻石商铺已更新":"诚信商铺已更新";
		$ADMIN->done_screen($msg, "商铺商品管理", "act=shop&code=edit" );

	}

	function view_goods()
	{
		global $INCOME,$DB,$ADMIN,$SKIN;
		$id = intval($INCOME['GOODS_ID']);
		$DB -> db_query("SELECT * FROM biz_goods ,biz_pic WHERE pic_id =goods_prime_picture_id  AND goods_id='".$id."' LIMIT 1");

		if( !$DB -> db_fetch_num()) $ADMIN -> error("没有返回该商品记录");
		
		$ADMIN -> page_title ="编辑商品";

		$SKIN -> td_header[] = array(" ","24%");
		$SKIN -> td_header[] = array(" ","");

        $ADMIN -> html = $SKIN -> table_head("基本信息");
		$ADMIN -> html .= $SKIN -> form_head();
		$ADMIN -> html .= $SKIN -> form_input("code","deditgoods","hidden");
		$ADMIN -> html .= $SKIN -> form_input("GOODS_ID",$id,"hidden");

		$res = $DB -> db_fetch_row();
        $path = $ADMIN->get_thumb($res['pic_name']);
        $ADMIN -> html .= $SKIN -> table_row( array("商品图片","<IMG SRC='$path' BORDER=0>" ) );
		$ADMIN -> html .= $SKIN -> table_row( array("商品名称",$SKIN -> form_input('goods_name',$res['goods_name']) ) );
		$ADMIN -> html .= $SKIN -> table_row( array("商品价格", $SKIN -> form_input('goods_price',$res['goods_price']) ) );
		$ADMIN -> html .= $SKIN -> table_row( array("商品折扣（百分比）",$SKIN -> form_input('goods_discount',$res['goods_discount'])) );
		$ADMIN -> html .= $SKIN -> table_row( array("商品描述","<textarea name='goods_describle' rows='15' cols='30'>".$res['goods_describle']."</textarea>"));
		$ADMIN -> html .= $SKIN -> table_row( array("商品推荐度",$SKIN->form_input("goods_global_recommend",$res['goods_global_recommend'])));
		$ADMIN -> html .= $SKIN -> form_end(" 修 改 ");
		$ADMIN -> html .= $SKIN -> table_end();
		
		$ADMIN -> output();
		return;
	}

	function do_edit_goods()
	{
		global $INCOME,$DB,$ADMIN;

		$id = intval($INCOME['GOODS_ID']);

		$DB -> db_query("SELECT goods_name FROM biz_goods WHERE goods_id='".$id."'");
		$r = $DB -> db_fetch_row();
		if( !$r['goods_name'] ) $ADMIN -> error("没有找到商品记录");

		$db_string = $DB-> db_compile_update_fields ( array(  "goods_name"      => $INCOME[goods_name] ,
				                                          "goods_price"  => $INCOME[goods_price],
				                                          "goods_discount" => $INCOME[goods_discount],
				                                          "goods_describle" => $INCOME[goods_describle],
				                                          "goods_global_recommend" => $INCOME[goods_global_recommend]                 
			)); 

		$db_query = "UPDATE biz_goods SET $db_string WHERE goods_id='".$id."'";

		$DB -> db_query($db_query);

		$ADMIN->record_log("修改商品‘{$r[goods_name]}’");
	    $ADMIN->done_screen("商品‘{$r[goods_name]}’已更新", "商铺商品管理", "act=shop&code=edit" );
	}

	function index()
	{
		global $ADMIN,$SKIN,$DB,$POSTER;
		$ADMIN -> page_title = "查看自助推广池商品商铺信息";
		$ADMIN -> page_instruction ="排队和正在推广的商品商铺。";

        $DB -> db_query("SELECT bg.goods_id,bg.goods_name,bop.end_time,bop.order_length FROM biz_goods as bg JOIN biz_order_pool as bop ON bg.goods_id = bop.goods_id WHERE type=".$POSTER['index_goods']['type']." ORDER BY active DESC");
		
		$SKIN -> td_header[] = array("商品ID","10%");
		$SKIN -> td_header[] = array("商品名","40%");
		$SKIN -> td_header[] = array("商品推广结束时间");
		$SKIN -> td_header[] = array("推广天数");

		$ADMIN -> html = $SKIN -> table_head("正在推广商品");
		while($r= $DB -> db_fetch_row() )
		{
			
			$date = ($r['end_time']>time())?date('Y-m-d H:i:s',$r['end_time']):"--";
			$ADMIN->html .= $SKIN -> table_row(array($r['goods_id'],$r['goods_name'],$date,$r['order_length']));
		}
        $ADMIN -> html .= $SKIN -> table_end();

		$ADMIN -> html .= $SKIN -> td_spacer();	

		$DB -> db_query("SELECT bs.shop_id,bs.shop_name,bop.end_time,bop.order_length FROM biz_shop as bs JOIN biz_order_pool as bop ON bs.shop_id = bop.shop_id WHERE type=".$POSTER['index_shop']['type']." ORDER BY active DESC");

		$SKIN -> td_header[] = array("商铺ID","10%");
		$SKIN -> td_header[] = array("商铺名","40%");
		$SKIN -> td_header[] = array("商铺推广结束时间");
		$SKIN -> td_header[] = array("推广天数");

		$ADMIN -> html .= $SKIN -> table_head("正在推广商铺");
		while($r= $DB -> db_fetch_row() )
		{
			$date = ($r['end_time']>time())?date('Y-m-d H:i:s',$r['end_time']):"--";
			$ADMIN->html .= $SKIN -> table_row(array($r['shop_id'],$r['shop_name'],$date,$r['order_length']));
		}
        $ADMIN -> html .= $SKIN -> table_end();
		$ADMIN -> output();
         
	}

}

?>